{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import warnings\n",
    "warnings.simplefilter('ignore')\n",
    "import numpy as np\n",
    "import scipy\n",
    "from scipy import stats\n",
    "import gc\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import matplotlib\n",
    "import copy\n",
    "import seaborn as sns\n",
    "import datetime\n",
    "import matplotlib.cm as cm\n",
    "import copy\n",
    "from tqdm.auto import tqdm\n",
    "from matplotlib import mlab\n",
    "import pylab\n",
    "from threading import Thread\n",
    "from multiprocessing import Process"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pol_rate(column):\n",
    "    \n",
    "    mean = column.mean()\n",
    "    \n",
    "    alpha_2 = column[column > mean].shape[0]\n",
    "    \n",
    "    p_2 = column[column > mean].mean()\n",
    "\n",
    "    alpha_1 = column[column < mean].shape[0]\n",
    "    \n",
    "    p_1 = column[column < mean].mean()\n",
    "    \n",
    "    pol_rate = 4 * alpha_2 * alpha_1 * (p_2 - p_1) / (alpha_2 + alpha_1) ** 2\n",
    "    \n",
    "    return (pol_rate, alpha_1 / (alpha_2 + alpha_1), p_1, p_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Data download"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>$p_{i}(t_{1})$</th>\n",
       "      <th>$p_{i}(t_{2})$</th>\n",
       "      <th>$p_{i}(t_{3})$</th>\n",
       "      <th>fr_number</th>\n",
       "      <th>$p_{-i}(t_{1})$</th>\n",
       "      <th>$p_{-i}(t_{2})$</th>\n",
       "      <th>$p_{-i}(t_{3})$</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.514611</td>\n",
       "      <td>0.514624</td>\n",
       "      <td>0.512251</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.410485</td>\n",
       "      <td>0.467247</td>\n",
       "      <td>0.457429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.186235</td>\n",
       "      <td>0.186235</td>\n",
       "      <td>0.186192</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.544541</td>\n",
       "      <td>0.576840</td>\n",
       "      <td>0.577952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.604282</td>\n",
       "      <td>0.603247</td>\n",
       "      <td>0.603549</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.333697</td>\n",
       "      <td>0.308934</td>\n",
       "      <td>0.286581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.608380</td>\n",
       "      <td>0.614673</td>\n",
       "      <td>0.637561</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.410863</td>\n",
       "      <td>0.413971</td>\n",
       "      <td>0.413348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.729980</td>\n",
       "      <td>0.722895</td>\n",
       "      <td>0.726959</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.605165</td>\n",
       "      <td>0.616444</td>\n",
       "      <td>0.614579</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   $p_{i}(t_{1})$  $p_{i}(t_{2})$  $p_{i}(t_{3})$  fr_number  $p_{-i}(t_{1})$  \\\n",
       "0        0.514611        0.514624        0.512251        7.0         0.410485   \n",
       "1        0.186235        0.186235        0.186192        7.0         0.544541   \n",
       "2        0.604282        0.603247        0.603549        6.0         0.333697   \n",
       "3        0.608380        0.614673        0.637561        3.0         0.410863   \n",
       "4        0.729980        0.722895        0.726959        3.0         0.605165   \n",
       "\n",
       "   $p_{-i}(t_{2})$  $p_{-i}(t_{3})$  \n",
       "0         0.467247         0.457429  \n",
       "1         0.576840         0.577952  \n",
       "2         0.308934         0.286581  \n",
       "3         0.413971         0.413348  \n",
       "4         0.616444         0.614579  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('X_opinions.csv')\n",
    "\n",
    "data.drop(['Unnamed: 0'], axis=1, inplace=True)\n",
    "\n",
    "data.head(5) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1660927"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Polarization rates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.25840282701549144\n",
      "0.2614988378207592\n",
      "0.26437692944690716\n"
     ]
    }
   ],
   "source": [
    "print(pol_rate(data['$p_{i}(t_{1})$'])[0])\n",
    "print(pol_rate(data['$p_{i}(t_{2})$'])[0])\n",
    "print(pol_rate(data['$p_{i}(t_{3})$'])[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ingridients of the polarization coefficient "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.25840282701549144, 0.4841741991068843, 0.34685331678544984, 0.6055152775341108)\n",
      "(0.2614988378207592, 0.47991151929013137, 0.3407184227607859, 0.602640051391381)\n",
      "(0.26437692944690716, 0.47855504787386804, 0.336236560882989, 0.6011007195738399)\n"
     ]
    }
   ],
   "source": [
    "print(pol_rate(data['$p_{i}(t_{1})$']))\n",
    "print(pol_rate(data['$p_{i}(t_{2})$']))\n",
    "print(pol_rate(data['$p_{i}(t_{3})$']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
